﻿using AutoMapper;
using B.S.Medical.Api.Write.Application.Command.Patient;
using B.S.Medical.Domain.Medical;
using B.S.Medical.ErrorCode;
using B.S.Medical.Infrastructure.Interface;
using MediatR;

namespace B.S.Medical.Api.Write.Application.Handler.Patient
{
    public class AddPatientCommandHandle : IRequestHandler<AddPatientCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<PatientModel> _patientrequest;
        private readonly IMapper mapper;

        public AddPatientCommandHandle(IBaseRepository<PatientModel> patientrequest, IMapper mapper)
        {
            _patientrequest = patientrequest;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(AddPatientCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model=mapper.Map<PatientModel>(request);
            model.WechatName = "";
            model.Avatar = "";
            model.CreateTime=DateTime.Now;
            model.AccountId = request.AccountId; // 关键：绑定当前登录用户Id
            var r=  await _patientrequest.CreateAsync(model);
            if(r>0)
            {
                res.Msg = "就诊人添加成功";
                res.Data = r;
                res.Code=ApiEnum.Success;
            }
            else
            {
                res.Msg = "就诊人添加失败";
                res.Data = r;
                res.Code = ApiEnum.Fail;

            }
            return await Task.FromResult(res);
        }
    }
}
